package cn.iocoder.yudao.module.system.controller.admin.qnaApplication;

import cn.iocoder.yudao.module.system.controller.admin.qnaApplication.vo.QnaApplicationPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.qnaApplication.vo.QnaApplicationRespVO;
import cn.iocoder.yudao.module.system.controller.admin.qnaApplication.vo.QnaApplicationSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.qnaApplication.QnaApplicationDO;
import cn.iocoder.yudao.module.system.service.qnaApplication.QnaApplicationService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getPortalLoginUserId;


@Tag(name = "管理后台 - 需求申请")
@RestController
@RequestMapping("/qna")
@Validated
public class QnaApplicationController {

    @Resource
    private QnaApplicationService applicationService;

    @PostMapping("/create")
    @Operation(summary = "创建需求申请")
    @PreAuthorize("@ss.hasPermission('qna:application:create')")
    public CommonResult<Integer> createApplication(@Valid @RequestBody QnaApplicationSaveReqVO createReqVO) {
        return success(applicationService.createApplication(createReqVO));
    }
    /**
     * 门户网站需求申请创建
     */
    @PostMapping("/portal/submit")
    @Operation(summary = "门户网站创建需求申请")
    public CommonResult<Integer> portalCreateApplication(@Valid @RequestBody QnaApplicationSaveReqVO createReqVO) {
        return success(applicationService.createApplication(createReqVO));
    }
    @PutMapping("/update")
    @Operation(summary = "更新需求申请")
    @PreAuthorize("@ss.hasPermission('qna:application:update')")
    public CommonResult<Boolean> updateApplication(@Valid @RequestBody QnaApplicationSaveReqVO updateReqVO) {
        applicationService.updateApplication(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除需求申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('qna:application:delete')")
    public CommonResult<Boolean> deleteApplication(@RequestParam("id") Integer id) {
        applicationService.deleteApplication(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除需求申请")
                @PreAuthorize("@ss.hasPermission('qna:application:delete')")
    public CommonResult<Boolean> deleteApplicationList(@RequestParam("ids") List<Integer> ids) {
        applicationService.deleteApplicationListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得需求申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('qna:application:query')")
    public CommonResult<QnaApplicationRespVO> getApplication(@RequestParam("id") Integer id) {
        QnaApplicationDO application = applicationService.getApplication(id);
        return success(BeanUtils.toBean(application, QnaApplicationRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得需求申请分页")
    @PreAuthorize("@ss.hasPermission('qna:application:query')")
    public CommonResult<PageResult<QnaApplicationRespVO>> getApplicationPage(@Valid QnaApplicationPageReqVO pageReqVO) {
        PageResult<QnaApplicationDO> pageResult = applicationService.getApplicationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult,QnaApplicationRespVO.class));
    }

    /**
     * 门户网站查询列表分页
     */
    @GetMapping("/portal/page")
    @Operation(summary = "门户网站查询列表分页")
    public CommonResult<PageResult<QnaApplicationRespVO>> getPortalApplicationPage(@Valid QnaApplicationPageReqVO pageReqVO) {
        pageReqVO.setCreateId(String.valueOf(getPortalLoginUserId()));
        PageResult<QnaApplicationDO> pageResult = applicationService.getApplicationPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult,QnaApplicationRespVO.class));
    }

    /**
     * 回复需求
     */
    @PostMapping("/reply")
    @Operation(summary = "回复需求")
    public CommonResult<Boolean> reply(@Valid QnaApplicationSaveReqVO updateReqVO) {
        applicationService.reply(updateReqVO);
        return success(true);
    }


    @GetMapping("/export-excel")
    @Operation(summary = "导出需求申请 Excel")
    @PreAuthorize("@ss.hasPermission('qna:application:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportApplicationExcel(@Valid QnaApplicationPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<QnaApplicationDO> list = applicationService.getApplicationPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "需求申请.xls", "数据", QnaApplicationRespVO.class,
                        BeanUtils.toBean(list, QnaApplicationRespVO.class));
    }

}